import searchBox from '@/components/search.vue';
import {mapState,mapMutations} from 'Vuex';

export default {
  name: "searchPage",
  data: () => ({
    searchArr:[],   //搜索记录列表

    localStorageArr:[],   //历史记录

    keyword:'',
    noneHouse:false,     //没有相关房源
  }),
  components:{
    searchBox,
  },
  computed:{
    ...mapState({
      searchValue:state=>state.house.searchKey,
    })
  },
  mounted() {
    //do something after mounting vue instance
    let that = this;
    that.keyword = that.searchValue==''? '':that.searchValue;
    console.log(that.keyword);
    that.getSearchHistory(that.keyword,((dataList)=>{
      console.log(dataList);
      that.localStorageArr = dataList;
    }));

  },
  methods: {
    ...mapMutations([
      'setSearchKey',
    ]),
    // 历史记录
    getSearchHistory(keyword,callback) {
      let searchParam = {
        keyword:keyword
      }
      network.get(configUrl.SEARCHHISTORYLIST_URL,searchParam,function(res){
        // console.log(res);
        // that.localStorageArr = res.dataList;
        callback(res.dataList)
      },function(err){
        console.log(err);
      });
    },
    // 清空搜索记录
    clearSearchHistory() {
      network.get(configUrl.DELETESEARCHHISTORY_URL,'',function(res){
        console.log(res);
        window.location.reload();   //刷新页面
      },function(err){
        console.log(err);
      })
    },
    // 匹配历史搜索记录
    searchKey(data) {
      let that = this;
      let searchValue = data.searchValue;   //搜索关键词
      that.getSearchHistory(searchValue,(dataList=>{
        console.log(dataList);
        // if(dataList.length>0){
        //   that.noneHouse = false;
          that.searchArr = dataList;
        // }else{
          // that.noneHouse = true;
        // }
      }));
    },
    // 点击搜索按钮进行查找
    searchEvent(data) {
      console.log(data);
      let searchValue = data.searchValue;   //搜索关键词
      this.$router.push({path:'/searchHouse',query:{rentType:1}})
    },
    // 列表选择搜索词
    searchList(itemKey) {
      this.setSearchKey(itemKey);
      this.$router.push({path:'/searchHouse',query:{rentType:1}});
    }
  }
}
